package com.share.thread;

/**
 * @desc:
 * @author:caifan
 * @date:2022/3/27
 */
public class ThreadLocalDemo04 {

    public static void main(String[] args) {
        new Service1().service1();
    }
}

class Service1 {

    public void service1() {
        User user = new User("admin");
        UserContextHolder.user.set(user);
        new Service2().service2();
    }
}

class Service2 {
    public void service2() {
        User user = UserContextHolder.user.get();
        System.out.println("Service2拿到用户名：" + user.name);
        new Service3().service3();
    }
}

class Service3 {

    public void service3() {
        User user = UserContextHolder.user.get();
        System.out.println("Service3拿到用户名：" + user.name);
        UserContextHolder.user.remove();
    }
}

class UserContextHolder {
    public static ThreadLocal<User> user = new ThreadLocal<>();
}

class User {
    String name;

    public User(String name) {
        this.name = name;
    }
}